﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace Peking_Duck
{
    class XMLParser
    {
        // This database information will be shared with QueryHandler
      public double _dbProgramVersion = 0.1; //TODO: fix to implement a read from projectversionsetting.
      public double _dbDocumentVersion = 2;
      public string _sDatabaseHost = "";
      public string _sDatabasePort = "";
      public string _sUser = "";
      public string _sPassword = "";
      public string _sCharacterDatabase = "";
      public string _sWorldDatabase = "";
        public void WriteXML(string Host, string Port, string Username, string Password, string CharacterDatabase, string WorldDatabase)
        { 
            XmlTextWriter tw = new XmlTextWriter("pekingduck.xml", null);
            tw.WriteStartDocument();
            tw.WriteStartElement("Configuration"); //begin Configuration
            tw.WriteStartElement("ProgramData");
            tw.WriteElementString("ProgramVersion", XmlConvert.ToString(_dbProgramVersion));
            tw.WriteElementString("DocumentVersion", XmlConvert.ToString(_dbDocumentVersion));
            tw.WriteEndElement(); //end ProgramData
            tw.WriteStartElement("UserData"); //where we store the user settings
            tw.WriteElementString("DatabaseHost", Host);
            tw.WriteElementString("DatabasePort", Port);
            tw.WriteElementString("Username", Username);
            tw.WriteElementString("Password", Password);
            tw.WriteElementString("CharacterDatabase", CharacterDatabase);
            tw.WriteElementString("WorldDatabase", WorldDatabase);
            tw.WriteEndElement(); //end UserData
            tw.WriteEndElement(); //end Configuration
            tw.WriteEndDocument();
            tw.Flush();
            tw.Close();
        }
        public void DatabaseSettingsModify(string sDatabaseHost, string sDatabasePort, string sUsername, string sPassword, string sCharacterDatabase, string sWorldDatabase)
        {
            _sDatabaseHost = sDatabaseHost;
            _sDatabasePort = sDatabasePort;
            _sUser = sUsername;
            _sPassword = sPassword;
            _sCharacterDatabase = sCharacterDatabase;
            _sWorldDatabase = sWorldDatabase;
            
        }
        public void ReadXML()        
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("pekingduck.xml");
            XmlNodeList _xmlDatabaseHost = doc.GetElementsByTagName("DatabaseHost");
            XmlNodeList _xmlDatabasePort = doc.GetElementsByTagName("DatabasePort");
            XmlNodeList _xmlUsername = doc.GetElementsByTagName("Username");
            XmlNodeList _xmlPassword = doc.GetElementsByTagName("Password");
            XmlNodeList _xmlCharacterDatabase = doc.GetElementsByTagName("CharacterDatabase");
            XmlNodeList _xmlWorldDatabase = doc.GetElementsByTagName("WorldDatabase");
            for (int i = 0; i < _xmlWorldDatabase.Count; ++i)
            {
                DatabaseSettingsModify(
                    _xmlDatabaseHost[i].InnerText,
                    _xmlDatabaseHost[i].InnerText,
                    _xmlUsername[i].InnerText,
                    _xmlPassword[i].InnerText,
                    _xmlCharacterDatabase[i].InnerText,
                    _xmlWorldDatabase[i].InnerText
                    );

            }
        }
    }
}
